# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Naveen M K <naveen521kk@gmail.com>

_realname=inkscape
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3
pkgrel=2
_pkg_suffix=1.3_2023-07-21_0e150ed6c4
pkgdesc="Vector graphics editor using the SVG file format (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://inkscape.org/"
license=("spdx:GPL-2.0-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cython")
depends=("${MINGW_PACKAGE_PREFIX}-aspell"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-gc"
         "${MINGW_PACKAGE_PREFIX}-gdl"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-gtkmm3"
         "${MINGW_PACKAGE_PREFIX}-gtkspell3"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lib2geom"
         "${MINGW_PACKAGE_PREFIX}-libcdr"
         "${MINGW_PACKAGE_PREFIX}-librevenge"
         "${MINGW_PACKAGE_PREFIX}-libsoup"
         "${MINGW_PACKAGE_PREFIX}-libvisio"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwpg"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-popt"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-scour"
         "${MINGW_PACKAGE_PREFIX}-omp")
optdepends=(#"${MINGW_PACKAGE_PREFIX}-pstoedit: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-texlive-scheme-medium: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: some extensions"
            "${MINGW_PACKAGE_PREFIX}-python-lxml: some extensions and filters"
            #"${MINGW_PACKAGE_PREFIX}-scour: optimized SVG output"
            #"${MINGW_PACKAGE_PREFIX}-uniconvertor: reading/writing to some proprietary formats"
            )
source=("https://media.inkscape.org/dl/resources/file/${_realname}-${pkgver}.tar.xz"
        inkscape-1.0.1-unbundle.patch
        inkscape-1.0.1-install-layout.patch)
sha256sums=('bf4f286b025e0169b8948cc14d5199a9b4c204d761c894c4b48496571ec76307'
            '92387251c1740f1a57cde9e587cc673ef1600813617bd2c9db65598c324a24d4'
            '131b2e1190637df0554ef1ee8cf46440689584375c117d057ab47d5871c58128')
noextract=("${_realname}-${pkgver}.tar.xz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  # Symlinks only in packaging/macos and src/3rdparty/cairo folders
  tar -xf ${_realname}-${pkgver}.tar.xz || true

  mv "${srcdir}"/${_realname}-${_pkg_suffix} "${srcdir}"/${_realname}-${pkgver}
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    inkscape-1.0.1-unbundle.patch \
    inkscape-1.0.1-install-layout.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  CFLAGS+=" -Wno-implicit-function-declaration" \
  MSYS=winsymlinks:native \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_TESTING=OFF \
    -DCMAKE_AR=${MINGW_PREFIX}/bin/ar \
    -DWITH_IMAGE_MAGICK=OFF \
    -DWITH_INTERNAL_CAIRO=OFF \
    -DWITH_INTERNAL_2GEOM=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
